package com.saxon.system.Generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * 自动生成Pojo,service,controller类
 *
 * @author saxon
 */
@Component
public class Generator {
    @Value ("${spring.datasource.url}")
    private String url;
    @Value ("${spring.datasource.driver-class-name}")
    private String driver;
    @Value ("${spring.datasource.username}")
    private String username;
    @Value ("${spring.datasource.password}")
    private String password;

    public void run () {
        AutoGenerator mgp = new AutoGenerator ();
        //全局配置
        GlobalConfig gc = new GlobalConfig ();
        //设置全局唯一ID
        gc.setIdType (IdType.ASSIGN_ID);
        //是否覆盖
        gc.setFileOverride (true);
        //设置日期格式
        gc.setDateType (DateType.ONLY_DATE);
        String projectPath = System.getProperty ("user.dir");
        gc.setOutputDir (projectPath + "/src/main/java");
        //设置文件作者
        gc.setAuthor ("saxon");
        //完成后是否打开文件夹
        gc.setOpen (false);
        gc.setServiceName ("%sService");
        //实体属性 Swagger2 注解
        gc.setSwagger2 (true);
        mgp.setGlobalConfig (gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig ();
        //数据库配置
        dsc.setUrl (url);
        dsc.setDriverName (driver);
        dsc.setUsername (username);
        dsc.setPassword (password);
        dsc.setDbType (DbType.MYSQL);
        mgp.setDataSource (dsc);
        // 包配置:设置想要创建的包
        PackageConfig pc = new PackageConfig ();
        //生成的文件夹就在这个目录下
        pc.setParent ("com.saxon.system");
        // pc.setModuleName ("blog"); //如果设置就会把生成的包放在这个报下
        //设置实体类的包名
        pc.setEntity ("pojo");
        //Dao的名字
        pc.setMapper ("mapper");
        //controller的名字
        pc.setController ("controller");
        //service的名字
        pc.setService ("service");
        mgp.setPackageInfo (pc);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig ();
        //下划线变成驼峰
        strategy.setNaming (NamingStrategy.underline_to_camel);
        //下划线变成驼峰
        strategy.setColumnNaming (NamingStrategy.underline_to_camel);
        // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");//再创建类的时候加上extends
        strategy.setEntityLombokModel (true);
        strategy.setRestControllerStyle (true);
        // 你要生成的表
        //strategy.setInclude ("sys_info_course", "sys_info_courseinfo", "sys_table_excuse", "sys_table_sign", "sys_table_statistics", "sys_table_status", "sys_user");
        strategy.setControllerMappingHyphenStyle (true);
        //设置逻辑删除功能
        strategy.setLogicDeleteFieldName ("deleted");
        //设置乐观锁插件
        strategy.setVersionFieldName ("version");
        TableFill createTime = new TableFill ("create_time", FieldFill.INSERT);
        TableFill modifyTime = new TableFill ("modify_time", FieldFill.UPDATE);
        strategy.setTableFillList (Arrays.asList (createTime, modifyTime));
        mgp.setStrategy (strategy);
        mgp.execute ();
    }
}

